package cn.lysj.framework.customer.service;

import cn.lysj.framework.base.BaseService;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.customer.domain.Customer;
import cn.lysj.framework.customer.domain.CustomerInfo;
import cn.lysj.framework.customer.domain.CustomerVip;
import cn.lysj.framework.customer.repository.CustomerRepository;
import cn.lysj.framework.customer.vo.CustomerVipInfoVo;
import cn.lysj.framework.verify.service.VerifyPhoneService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zwj
 * @description 客户类服务层
 * @create 2018-11-13 14:22:44
 **/
@Slf4j
@Service
@Transactional
public class CustomerService implements BaseService<Customer> {

    @Resource
    private CustomerRepository customerRepository;

    @Resource
    private CustomerInfoService customerInfoService;

    @Resource
    private CustomerVipService customerVipService;

    @Resource
    private VerifyPhoneService verifyPhoneService;

    @Override
    public CustomerRepository getRepository() {
        return customerRepository;
    }


    /**
     * @author ZhangWenJian
     * @date 2019/1/15 11:51
     * @title customerBindingCustomerInfo
     * @param customerInfoId
     * @param customerId
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 数据库中拥有启用状态的用户信息时数据绑定的方法
     */
    public Resp bingingCustomerInfo(String customerInfoId,String customerId){
        if(ParamUtil.isBlank(customerInfoId)){
            return new Resp(Resp.PARAM_ERROR,"绑定失败,绑定对象信息有误!");
        }
        CustomerInfo customerInfo = customerInfoService.getRepository().findOne(customerInfoId);
        if(customerInfo == null){
            return new Resp(Resp.PARAM_ERROR,"绑定失败,绑定对象信息有误!");
        }
        if(ParamUtil.isBlank(customerId)){
            return new Resp(Resp.PARAM_ERROR,"绑定失败,用户信息有误!");
        }
        Customer customer = customerRepository.findOne(customerId);
        if(customer == null){
            return new Resp(Resp.PARAM_ERROR,"绑定失败,用户信息有误!");
        }
        if(!ParamUtil.isBlank(customer.getCustomerInfoId())){
            return new Resp(Resp.PARAM_ERROR,"您已经绑定了信息,请勿重复操作!");
        }
        customer.setCustomerInfoId(customerInfoId);
        customerRepository.save(customer);
        return new Resp(customerInfo,"绑定成功!");
    }
    /**
     * @author ZhangWenJian
     * @date 2019/1/21 9:20
     * @title customerBuildCustomerInfo
     * @param storeId
     * @param customerId
     * @param name
     * @param phone
     * @param verifyCode
     * @param birthday
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 绑定用户信息 如果有该信息提示前端是否进行绑定 没有则创建用户信息记录,以及创建会员记录.
     */
    public Resp newCustomerInfo(String storeId, String customerId, String name, String phone,String verifyCode, Date birthday){
        //验证手机验证码
        Resp resp = verifyPhoneService.phoneVerify(phone,verifyCode);
        if (resp.getCode() != 200){
            return resp;
        }
        Customer customer = customerRepository.findOne(customerId);
        if(customer == null){
            return new Resp(Resp.PARAM_ERROR,"数据异常!");
        }
        if(!ParamUtil.isBlank(customer.getCustomerInfoId())){
            return new Resp(Resp.PARAM_ERROR,"您已经是会员了,请勿重新绑定!");
        }
        CustomerInfo customerInfo1 = customerInfoService.getRepository().findByPhone(phone);

        Map<String,Object> resultMap = new HashMap<>();
        //如果数据库没有对应数据则创建 直接绑定信息以及vip
        if(customerInfo1 == null){
            //创建用户信息
            CustomerInfo customerInfo = new CustomerInfo(name,phone,birthday);
            customerInfo = customerInfoService.getRepository().saveAndFlush(customerInfo);
            //用户子账户标识主信息
            customer.setCustomerInfoId(customerInfo.getId());
            customerRepository.save(customer);
            //创建用户与商店的会员关系
            CustomerVip customerVip = customerVipService.buildCustomerVip(storeId,customerInfo.getId());
            resultMap.put("customerVipType",1);
            resultMap.put("customerInfo",customerInfo);
            return new Resp(resultMap,"信息绑定成功!");
        }
        //数据库已经有对应用户信息 前端展示提示用户是否进行绑定
        else{
            resultMap.put("customerVipType",2);
            resultMap.put("customerInfoId",customerInfo1.getId());
            return new Resp(resultMap,"该手机号已有绑定信息,是否关联该手机?注意,绑定该手机,名字,生日等信息将不会修改.");
        }

    }
}
